package com.platform.modules.prj.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.activiti.engine.delegate.DelegateExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Query;
import com.platform.modules.act.ctx.entity.ProcessObject;
import com.platform.modules.act.ctx.service.ProcessObjectService;
import com.platform.modules.cpt.service.impl.CptAcceptanceConditionServiceImpl;
import com.platform.modules.prj.dao.PrjPauseDao;
import com.platform.modules.prj.entity.PrjPauseEntity;
import com.platform.modules.prj.service.PrjPauseService;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysUserEntity;

/**
 * Service实现类
 *
 * @author biaohuaming
 * @date 2020-03-11
 */
@Service("prjPauseService")
public class PrjPauseServiceImpl extends ServiceImpl<PrjPauseDao, PrjPauseEntity> implements PrjPauseService {
	
    private static final Logger logger = LoggerFactory.getLogger(PrjPauseServiceImpl.class);
	
	@Autowired
	private ProcessObjectService processObjectService;

	@Override
	public Page<PrjPauseEntity> queryPage(Map<String, Object> params) {    	
        params.put("sidx", "A.CREATE_TIME");
        params.put("asc", false);
        Page<PrjPauseEntity> page = new Query<PrjPauseEntity>(params).getPage();
        List<PrjPauseEntity> list = baseMapper.queryPage(page, params);
        return page.setRecords(list);
	}

	@Override
	public PrjPauseEntity sp(PrjPauseEntity prjPauseEntity, SysOrgEntity sysOrgEntity, SysUserEntity sysUserEntity) {
		PrjPauseEntity queryEntity = baseMapper.selectById(prjPauseEntity.getId());
		if(null==queryEntity) {
			throw new BusinessException("未找到中止施工申请");
		}
		if(null!=prjPauseEntity.getStatus()) {
			queryEntity.setStatus(prjPauseEntity.getStatus());
		}
		queryEntity.setSuperviseOrgId(sysOrgEntity.getOrgNo());
		queryEntity.setSuperviseOrgName(sysOrgEntity.getOrgName());
		queryEntity.setSuperviseUserId(sysUserEntity.getUserId());
		queryEntity.setSuperviseUserName(sysUserEntity.getUserName());
		queryEntity.setUpdateTime(new Date());
		queryEntity.setRejectReason(prjPauseEntity.getRejectReason());
		queryEntity.setConsentPauseDate(prjPauseEntity.getConsentPauseDate());
		baseMapper.updateById(queryEntity);
		return queryEntity;
	}

	@Override
	public void updateStatus(DelegateExecution execution) {
//		{"taskId":"4340092","procDefId":"","poId":"a0b191a712b241fab9e2c29c3d75e784",
//			"variables":[{"name":"inclusivegateway1","value":""},{"name":"jszzsgsq","value":""},
//			             {"name":"endevent1","value":""},{"name":"poId","value":"a0b191a712b241fab9e2c29c3d75e784"}],
//			"opinion":{"pass":"endevent1","passName":"End","comments":""}}
		String taskDefName = execution.getCurrentActivityName();
		System.out.println("taskDefName"+taskDefName);
		
		String jszzsgsq = (String) execution.getVariable("jszzsgsq");
		System.out.println("jszzsgsq:"+execution.getVariable("jszzsgsq"));
		
		String endevent1 = (String) execution.getVariable("endevent1");
		System.out.println("endevent1:"+execution.getVariable("endevent1"));
		
		String poId = (String) execution.getVariable("poId");
		System.out.println("poId:"+execution.getVariable("poId"));
		
		String userId = (String) execution.getVariable("userId");
		System.out.println("userId:"+execution.getVariable("userId"));
		
		ProcessObject processObject = processObjectService.getById(poId);
		if(null==processObject) {
			logger.error("未找到工作流业务对象poId:{}",poId);
			return ;
		}
		
		PrjPauseEntity prjPauseEntity = baseMapper.selectById(processObject.getBusinessId());
		if(null==prjPauseEntity) {
			logger.error("未找到工作流业务对象businessId:{}",processObject.getBusinessId());
			return ;
		}
		
		//同意(1-已申请 2-已驳回 3-已同意)
		if("false".equals(jszzsgsq)) {
			prjPauseEntity.setStatus(3);
		}else {
			//驳回
			prjPauseEntity.setStatus(2);
		}
		prjPauseEntity.setUpdateTime(new Date());
//		baseMapper.updateById(prjPauseEntity);

	}

	@Override
	public void start(DelegateExecution execution) {
		String poId = (String) execution.getVariable("poId");
		System.out.println("poId:"+execution.getVariable("poId"));
		ProcessObject processObject = processObjectService.getById(poId);
		if(null==processObject) {
			logger.error("未找到工作流业务对象poId:{}",poId);
			return ;
		}
		
		PrjPauseEntity prjPauseEntity = baseMapper.selectById(processObject.getBusinessId());
		if(null==prjPauseEntity) {
			logger.error("未找到工作流业务对象businessId:{}",processObject.getBusinessId());
			return ;
		}
		
		//同意(0-未申请 1-已申请 2-已驳回 3-已同意)
		prjPauseEntity.setStatus(1);
		prjPauseEntity.setUpdateTime(new Date());
		baseMapper.updateById(prjPauseEntity);
	}
	
}
